/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》  http://lblbc.cn/note
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 */
const { sequelize } = require('@core/db')
const { Goods } = require('@models/goods')
const { QueryTypes } = require('sequelize');

class GoodsDao {
  static async queryGoodsByCategory(categoryId) {
    try {
      const result = await sequelize.query("SELECT g.id,g.name,g.price,g.square_pic squarePic,g.description FROM m_goods_category gc,m_goods g WHERE gc.goods_id=g.id AND gc.category_id=" + categoryId, { type: QueryTypes.SELECT });
      return [null, result]
    } catch (err) {
      return [err, null]
    }
  }

  static async searchGoods(keyword) {
    try {
      const result = await sequelize.query("select g.id,g.name,g.price,g.square_pic squarePic,g.description from m_goods g where name like '%" + keyword+"%'", { type: QueryTypes.SELECT });
      return [null, result]
    } catch (err) {
      return [err, null]
    }
  }

  static async queryGoods(goodsId) {
    try {
      const result = await sequelize.query("select g.id,g.name,g.price,g.square_pic squarePic,g.description from m_goods g where id=" + goodsId, { type: QueryTypes.SELECT });
      return [null, result[0]]
    } catch (err) {
      return [err, null]
    }
  }
}

module.exports = {
  GoodsDao
}
